package 字符串通配;

import java.util.Scanner;

/**
 * Created by hest0 on 2017/4/18.
 */
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            String A = scanner.next();
            int m = scanner.nextInt();
            String B = scanner.next();
            System.out.println(new WildMatch1().chkWildMatch(A, n, B, m));
            System.out.println(new WildMatch2().chkWildMatch(A, n, B, m));
        }
    }

    /**
     * 动态规划的方法解决问题
     */
    public static class WildMatch1 {
        public boolean chkWildMatch(String A, int lena, String B, int lenb) {
            // d[i][j]表示A中的1~i位可以匹配B中的1~j位
            boolean[][] dp = new boolean[lena + 1][lenb + 1];
            // 初始化
            dp[0][0] = true;

            for (int i = 1; i <= lena; i++) {
                for (int j = 1; j <= lenb; j++) {
                    if (B.charAt(j - 1) == '*') {
                        //取上左
                        dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
                    } else if (B.charAt(j - 1) == '.') {
                        //取对角
                        dp[i][j] = dp[i - 1][j - 1];
                    } else {
                        dp[i][j] = dp[i - 1][j - 1] && (A.charAt(i - 1) == B.charAt(j - 1));
                    }
                }
            }
            return dp[lena][lenb];
        }
    }

    /**
     * 递归的方法解决问题
     */
    public static class WildMatch2 {
        private char temp;

        public boolean chkWildMatch(String A, int lena, String B, int lenb) {
            if (lena == 0) {
                if (lenb == 0 || "*".equals(B))
                    return true;
                return false;
            }
            if (A.charAt(0) == B.charAt(0) || '.' == B.charAt(0)) {
                temp = B.charAt(0);
                return chkWildMatch(A.substring(1), lena - 1, B.substring(1), lenb - 1);
            }
            if ('*' == B.charAt(0)) {
                if (A.charAt(0) == temp || '.' == temp) {
                    //temp = A.charAt(0);
                    return chkWildMatch(A.substring(1), lena - 1, B, lenb) ||
                            chkWildMatch(A.substring(1), lena - 1, B.substring(1), lenb - 1);
                } else {
                    return chkWildMatch(A, lena, B.substring(1), lenb - 1);
                }
            }
            return false;
        }
    }
}
